// 装商品图片的数组
let lb = []

// 装文字图片的数组
let wz = []




//获取id
function hqid() {
    let url = location.href
    let scx = url.indexOf('?')
    let dsfg = url.substring(scx + 1)
    let id = dsfg.split('=')[1]
    return id
}
// 获取商品数据
function hqsj() {
    let id = hqid()
    let sum = 1
    $.ajax({
        url: 'http://www.xiongmaoyouxuan.com/api/detail',
        type: 'get',
        data: {
            id
        },
        success: function(result) {
            let box = ''
            result.data.detail.photo.forEach(item => {
                lb.push(item)
            })
            result.data.detail
                // 获取商品名字
            wz.push({ title: result.data.detail.title })
                // 获取商品原价
            wz.push({ originPrice: result.data.detail.originPrice })
                // 获取折后价
            wz.push({ price: result.data.detail.price })
                // 获取优惠券
            wz.push({ couponValue: result.data.detail.couponValue })
                // 多少人购买
            wz.push({ saleNum: result.data.detail.saleNum })
                // 有效时间
            wz.push({ expireDate: result.data.detail.expireDate })
                // 获取id
            wz.push({ id: id })
                // 获取tup
            wz.push({ url: result.data.detail.photo[0].url })
                // 获取数量
            wz.push({ sum: sum })

            document.querySelector('.show-box>img').setAttribute('src', result.data.detail.photo[0].url)
            document.querySelector('.right>img').setAttribute('src', result.data.detail.photo[0].url)
            result.data.detail.photo.forEach(item => {
                box += ` <li data-index="1"><img src="${item.url}" alt=""></li>`

            })
            console.log(box)
            document.querySelector('.tu11').innerHTML = box
            let glass1 = new GlassZoom('#glassone')
            glass1.moveGlass()
            glass1.onTabGlass()

            xrlb()
            xrwz()
        }
    })
}
// 渲染商品图
function xrlb() {
    su = {
        list: [...lb]
    }

    let html = template('see', su)
    document.querySelector('.xia').innerHTML = html

}

// 单击小图片更换大图片
function htp(e) {
    document.querySelector('.ul').addEventListener('click', (e) => {
        if (e.target.nodeName == "IMG") {
            document.querySelector('.dt').setAttribute('src', e.target.getAttribute('src'))
        }
    })

}
// 渲染文字数据
function xrwz() {
    $('.spwz').html(wz[0].title)
    $('i').html('原价' + wz[1].originPrice)
    $('.zhj').html(wz[2].price)
    $('small').html(wz[3].couponValue)
    $('h2').html(wz[4].saleNum + '人已买')
    $('.sq').html('优惠有效期：' + wz[5].expireDate)
}

// 返回顶部功能实现
$('.plug-in').on('click', function() {
    let box1 = document.documentElement.scrollTop
    let box = setInterval(() => {
        document.documentElement.scrollTop = box1
        box1 -= 20
        if (box1 <= 0) {
            clearInterval(box)
        }
    }, 1)
})

// 滚动距离到xxx显示返回顶部按钮
window.onscroll = function() {
    if (window.scrollY < 400) {
        $('.plug-in').addClass('yyy')
    }
    if (window.scrollY >= 400) {
        $(".plug-in").removeClass('yyy');
    }
}

function hsy() {
    location.href = './index.html'
}

function xians() {
    $('.show-box').on('mouseover', function() {
        $('.right').removeClass('yyy');
    })
    $('.show-box').on('mouseout', function() {
        $('.right').addClass('yyy')
    })
}

function jrgwc() {
    let box1 = {
        title: wz[0].title,
        id: wz[6].id,
        url: wz[7].url,
        num: 1,
        dj: wz[2].price
    }
    let box = box1
    let loca = localStorage.getItem("list");
    let objloca = JSON.parse(loca) || [];
    let boo = false;
    objloca.forEach((item) => {
        if (item.id == box.id) {
            item.num++;
            boo = true;
        }
    });
    if (boo) {} else {
        objloca.push(box);

    }
    localStorage.setItem("list", JSON.stringify(objloca));
    location.href = './shopping-che.html'
}

xians()
hqsj()